package com.fare.taluo.application.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.hbase.filter.Filter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.GlobalsConst;
import com.fare.taluo.application.FreightPriceApplication;
import com.fare.taluo.application.fetl.DoublePlaneInfoApplication;
import com.fare.taluo.application.fetl.SinglePlaneInfoApplication;
import com.fare.taluo.core.domain.entity.crawl.FreightPriceEntity;
import com.fare.taluo.fetl.domain.entity.AirChinaInternationalEntity;
import com.fare.taluo.fetl.domain.entity.LineDayEntity;
import com.fare.taluo.fetl.domain.entity.SinglePlaneInfoEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.AgentEntityVO;
import com.fare.taluo.vo.core.CabinEntityVO;
import com.fare.taluo.vo.core.FreightPriceEntityVO;
import com.fare.taluo.vo.core.HBaseFilterVO;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.vo.core.PriceEntityVO;
import com.fare.taluo.vo.core.ReturnTripEntityVO;
import com.fare.taluo.vo.core.TransitchangeEntityVO;
import com.foreveross.crawl.domain.freight.AgentEntity;
import com.foreveross.crawl.domain.freight.CabinEntity;
import com.foreveross.crawl.domain.freight.PriceEntity;
import com.foreveross.crawl.domain.freight.ReturnTripEntity;
import com.foreveross.crawl.domain.freight.TransitchangeEntity;
@Transactional
@Scope("singleton")
@Service("freightPriceApplication")
public class FreightPriceApplicationImpl implements FreightPriceApplication {

	@Override
	public List<FreightPriceEntityVO> findByConditions(String tableName,
			String startRowKey, String endRowKey) {
		FreightPriceEntity entity = new FreightPriceEntity();
		List<FreightPriceEntity> entityList = null;
		try {
		    entityList = entity.findByConditions(tableName, startRowKey, endRowKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<FreightPriceEntityVO> voList = new ArrayList<FreightPriceEntityVO>();
		for (FreightPriceEntity freightPriceEntity : entityList) {
			FreightPriceEntityVO vo = new FreightPriceEntityVO();
			vo.setAgentName(freightPriceEntity.getAgentName());
			vo.setAgents(this.AgentEntityToVO( freightPriceEntity.getAgents()));
			vo.setAreaCode(freightPriceEntity.getAreaCode());
			vo.setAreaName(freightPriceEntity.getAreaName());
			vo.setAttachHbaseKey(freightPriceEntity.getAttachHbaseKey());
			
			vo.setCabins(this.sortCabinsByPrice(this.CabinEntityToVO(freightPriceEntity.getCabins())));
			vo.setCarrierFullName(freightPriceEntity.getCarrierFullName());
			vo.setCarrierKey(freightPriceEntity.getCarrierKey());
			vo.setCarrierName(freightPriceEntity.getCarrierName());
			vo.setCreateTime(freightPriceEntity.getCreateTime());
			vo.setSourceRowkey(freightPriceEntity.getSourceRowkey().substring(0, 10)+freightPriceEntity.getGrabChannelId()+freightPriceEntity.getAreaCode()
					+freightPriceEntity.getFromCity()+freightPriceEntity.getToCity()+DateUtils.formatDay(freightPriceEntity.getStartTime(), "yyyyMMddHH")
					+freightPriceEntity.getFlightId()+"("+"0000000"+"-ZZZZZZZ"+")");
			vo.setEndTime(freightPriceEntity.getEndTime());
			vo.setFlightDate(freightPriceEntity.getFlightBatchDate());
			vo.setFlightNo(freightPriceEntity.getFlightId());
			vo.setFlightType(freightPriceEntity.getFlightType());
			vo.setFromCity(freightPriceEntity.getFromCity());
			vo.setFromCityName(freightPriceEntity.getFromCityName());
			vo.setGrabChannel(freightPriceEntity.getGrabChannel());
			vo.setGrabChannelId(freightPriceEntity.getGrabChannelId());
			vo.setHighPrice(freightPriceEntity.getHighPrice());
			vo.setLowerPrice(freightPriceEntity.getLowerPrice());
			vo.setPrices(this.PriceEntityToVO(freightPriceEntity.getPrices()));
			//vo.setSourceRowkey(freightPriceEntity.getSourceRowkey());
			vo.setStartTime(freightPriceEntity.getStartTime());
			vo.setToCity(freightPriceEntity.getToCity());
			vo.setToCityName(freightPriceEntity.getToCityName());
			vo.setReturnTrip(this.ReturnTripEntityToVO(freightPriceEntity.getReturnTrip()));
			vo.setTransit(this.TransitchangeEntityToVO(freightPriceEntity.getTransit()));
			voList.add(vo);
		}
		
		return voList;
		
	}
	
	@SuppressWarnings("unchecked")
	private List<CabinEntityVO> sortCabinsByPrice(List<CabinEntityVO> voList){
		Collections.sort(voList,new CabinComparator());
		return voList;
	}
	
	private List<ReturnTripEntityVO> ReturnTripEntityToVO(List<ReturnTripEntity> entityList){
		List<ReturnTripEntityVO> voList = new ArrayList<ReturnTripEntityVO>();
		for (ReturnTripEntity entity : entityList) {
			ReturnTripEntityVO vo = new ReturnTripEntityVO();
			vo.setCarrierFullName(entity.getCarrierFullName());
			vo.setCarrierKey(entity.getCarrierKey());
			vo.setCarrierName(entity.getCarrierName());
			vo.setEndTime(entity.getEndTime());
			vo.setFlightDate(entity.getFlightDate());
			vo.setFlightNo(entity.getFlightNo());
			vo.setFromAir(entity.getFromAir());
			vo.setFromCity(entity.getFromCity());
			vo.setFromCityName(entity.getFromCityName());
			vo.setStartTime(entity.getStartTime());
			vo.setToAir(entity.getToAir());
			vo.setToCity(entity.getToCity());
			vo.setToCityName(entity.getToCityName());
			vo.setTotalPrice(entity.getTotalPrice());
			//vo.setTransit(entity.getTransit());
			voList.add(vo);
			
			
			
		}
		
		
		return voList;
	}
	
	private List<TransitchangeEntityVO> TransitchangeEntityToVO(List<TransitchangeEntity> entityList){
		List<TransitchangeEntityVO> voList = new ArrayList<TransitchangeEntityVO>();
		for (TransitchangeEntity entity : entityList) {
			TransitchangeEntityVO vo = new TransitchangeEntityVO();
			vo.setEndTime(entity.getEndTime());
			vo.setFlightTime(entity.getFlightTime());
			vo.setFlightTyte(entity.getFlightTyte());
			vo.setFromAir(entity.getFromAir());
			vo.setFromCity(entity.getFromCity());
			vo.setFromCityCode(entity.getFromCityCode());
			vo.setHightPrice(entity.getHightPrice());
			vo.setLowerPrice(entity.getLowerPrice());
			vo.setStartTime(entity.getStartTime());
			vo.setToAir(entity.getToAir());
			vo.setToCity(entity.getToCity());
			vo.setToCityCode(entity.getToCityCode());
			vo.setTransitFullName(entity.getCarrierFullName());
			vo.setTransitName(entity.getCarrierName());
			vo.setTransitCarrierKey(entity.getCarrierKey());
			voList.add(vo);	
			
		}
		
		return voList;
	}

	
    private List<PriceEntityVO> PriceEntityToVO(Set<PriceEntity> entitySet){
      	List<PriceEntityVO> voList = new ArrayList<PriceEntityVO>();
	    	for (PriceEntity entity : entitySet) {
	    		PriceEntityVO vo = new PriceEntityVO();
	    		vo.setKindName(entity.getKindName());
	    		vo.setKindNo(entity.getKindNo());
	    		vo.setPrice(entity.getPrice());
	    		voList.add(vo);  		
			}
	    	return voList;
	    }
	
	
    private List<AgentEntityVO> AgentEntityToVO(Set<AgentEntity> entitySet){
    	List<AgentEntityVO> voList = new ArrayList<AgentEntityVO>();
    	for (AgentEntity entity : entitySet) {
    		AgentEntityVO vo = new AgentEntityVO();
    		vo.setName(entity.getName());
    		vo.setOriginalPrice(""+entity.getOriginalPrice());
    		vo.setPrice(""+entity.getPrice());
    		voList.add(vo);  		
		}
    	
    	return voList;
    }
    
    private List<CabinEntityVO> CabinEntityToVO(Set<CabinEntity> entitySet){
    	List<CabinEntityVO> voList = new ArrayList<CabinEntityVO>();
    	for (CabinEntity entity : entitySet) {
    		CabinEntityVO vo = new CabinEntityVO();
    		vo.setCabinName(entity.getCabinName());
    		vo.setCabinNo(entity.getCabinNo());
    		vo.setCabinRebate(entity.getCabinRebate());
    		vo.setCabinServer(entity.getCabinServer());
    		vo.setPrice(entity.getPrice()==null?0:Double.valueOf(entity.getPrice()));
    		vo.setCabinType(entity.getCabinType());
    		vo.setLever(entity.getLevel()+"");
    		voList.add(vo);  		
		}
    	return voList;
    }
   
	@Override
	public List<PlaneInfo> findByConditionsToPlaneInfo(String tableName,
			String startRowKey, String endRowKey, List<Filter> filterList) {
		List<PlaneInfo> list = null;
	//	List<PlaneInfo> yesterdayList = null;
	//	Map<String,List<CabinEntityVO>> map = new HashMap<String, List<CabinEntityVO>>();
		//String yesterdayStartRowKey = this.getYesterdayRowKey(startRowKey);
		//String yesterdayEndRowKey = this.getYesterdayRowKey(endRowKey);
		FreightPriceEntity entity = new FreightPriceEntity();
		//if(StringAndDateAndFileUtil.checkString(yesterdayStartRowKey)&&StringAndDateAndFileUtil.checkString(yesterdayEndRowKey)){
			//yesterdayList = entity.findByConditionsToPlaneInfo(tableName, yesterdayStartRowKey, yesterdayEndRowKey, filterList);
		//	yesterdayList = entity.findByCustomFilters(tableName, yesterdayStartRowKey, yesterdayEndRowKey,null, filterList);
		
		//}
		//list = entity.findByConditionsToPlaneInfo(tableName, startRowKey, endRowKey, filterList);
		if(tableName.indexOf("@LP_FLIGHT")!=-1 && tableName.indexOf("LP_FLIGHT_DOUBLE")==-1){ //当前 单程的
			SinglePlaneInfoApplication singlePlaneInfoApplication =InstanceFactory.getInstance(SinglePlaneInfoApplication.class);
			String flightDateStr = null;
			if(startRowKey.length()>=27){
				flightDateStr=	startRowKey.substring(11, 19);
				if(!flightDateStr.startsWith("00") ){
					flightDateStr= DateUtils.dateToStr(DateUtils.strToDate(flightDateStr, "yyyyMMdd"), "yyyy-MM-dd");
				}
			}
			
			
			List<PlaneInfo> listVo=singlePlaneInfoApplication.findPlaneInfo(startRowKey.substring(0, 5), startRowKey.substring(5, 8), startRowKey.substring(8,11), flightDateStr, null);
			List<PlaneInfo> yesterdayList = singlePlaneInfoApplication.findPlaneInfo(startRowKey.substring(0, 5), startRowKey.substring(5, 8), startRowKey.substring(8,11), this.getDate(flightDateStr), null);
			list=this.getTendencyList(listVo, yesterdayList);
		}else if(tableName.indexOf("LP_FLIGHT_DOUBLE")!=-1){ // 当前 往返的
			DoublePlaneInfoApplication doublePlaneInfoApplication =InstanceFactory.getInstance(DoublePlaneInfoApplication.class);
			String flightDateStr =null;
			String returnDateStr =null;
			if(startRowKey.length()>=27){
				flightDateStr = startRowKey.substring(11, 19);
				if(!flightDateStr.startsWith("00")){
					flightDateStr= DateUtils.dateToStr(DateUtils.strToDate(flightDateStr, "yyyyMMdd"), "yyyy-MM-dd");
				}
				
				returnDateStr = startRowKey.substring(19, 27);
				if(!returnDateStr.startsWith("00")){
					returnDateStr=DateUtils.dateToStr(DateUtils.strToDate(returnDateStr, "yyyyMMdd"), "yyyy-MM-dd");
				}
			}
			
			List<PlaneInfo> listVo=doublePlaneInfoApplication.findPlaneInfo(startRowKey.substring(0, 5),startRowKey.substring(5, 8),
				startRowKey.substring(8, 11),flightDateStr,returnDateStr, null);
			List<PlaneInfo> yesterdayList = doublePlaneInfoApplication.findPlaneInfo(startRowKey.substring(0, 5),startRowKey.substring(5, 8),
					startRowKey.substring(8, 11), this.getDate(flightDateStr), this.getDate(returnDateStr), null);
			list=this.getTendencyList(listVo, yesterdayList);
		}else{
			list = entity.findByCustomFilters(tableName, startRowKey, endRowKey, null, filterList);
		}
		
	/*	if(yesterdayList != null && yesterdayList.size()>0){
			for (PlaneInfo planeInfo : list) {
				String currentHour = planeInfo.getFlightBatchStartTime().substring(8, 10);	
				String currentFlightNo = planeInfo.getFlightId();
				double cPrice = Double.parseDouble(planeInfo.getLowerPrice());
				boolean isFlightNoEqual = false;
				for(PlaneInfo yesterdayPlane : yesterdayList){
					double yPrice = Double.parseDouble(yesterdayPlane.getLowerPrice());//昨天最低价
					String yflightHour= yesterdayPlane.getFlightBatchStartTime().substring(8, 10);//20130722150000->2013072215
					String yFlightNo = yesterdayPlane.getFlightId();
					if(yFlightNo.equals(currentFlightNo) && yflightHour.equals(currentHour)){
						isFlightNoEqual = true;
						if(cPrice < yPrice){
							//小于前一天,下降
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_DESCEND);
						}else if(cPrice > yPrice){
							//大于前一天,上升
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_ASCEND);
						}else {
							//等于前一天,持平
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
						}
					}
						
				}
				if(isFlightNoEqual==false){
					planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
				}
			
			}
		}else{
			//没有数据运价走势持平
 			
				for (PlaneInfo planeInfo : list) {
				planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
			}
		}
		
		
		//对仓位重新组织，将cabin_type一样的仓位归类
		for (PlaneInfo planeInfo : list) {
			List<CabinEntityVO> cabins = planeInfo.getCabins();
			if(cabins != null && cabins.size()>0){
				for (CabinEntityVO cabinEntityVO : cabins) {
					if(map.containsKey(cabinEntityVO.getCabinType())){
						map.get(cabinEntityVO.getCabinType()).add(cabinEntityVO);
					}else{
						List<CabinEntityVO> putList = new ArrayList<CabinEntityVO>();
						putList.add(cabinEntityVO);
						map.put(cabinEntityVO.getCabinType(), putList);
					}
				}
			}
			
			//重新放到
			Set<Map.Entry<String, List<CabinEntityVO>>> set = map.entrySet();
	        for (Iterator<Map.Entry<String, List<CabinEntityVO>>> it = set.iterator(); it.hasNext();) {
	            Map.Entry<String, List<CabinEntityVO>> entry = (Map.Entry<String, List<CabinEntityVO>>) it.next();
	            planeInfo.getClassifyCabins().add(entry.getValue());
                //  System.out.println(entry.getKey() + "--->" + entry.getValue());
	        }		
			map.clear();
		}*/
		
		
		return list;
		
	}

	

	private List<PlaneInfo> getTendencyList(List<PlaneInfo> list,
			List<PlaneInfo> yesterdayList) {
		Map<String,List<CabinEntityVO>> map = new HashMap<String, List<CabinEntityVO>>();
		if(yesterdayList != null && yesterdayList.size()>0){
			for (PlaneInfo planeInfo : list) {
				String currentHour = planeInfo.getFlightBatchStartTime().substring(8, 10);	
				String currentFlightNo = planeInfo.getFlightId();
				double cPrice = Double.parseDouble(planeInfo.getLowerPrice());
				boolean isFlightNoEqual = false;
				for(PlaneInfo yesterdayPlane : yesterdayList){
					double yPrice = Double.parseDouble(yesterdayPlane.getLowerPrice());//昨天最低价
					String yflightHour= yesterdayPlane.getFlightBatchStartTime().substring(8, 10);//20130722150000->2013072215
					String yFlightNo = yesterdayPlane.getFlightId();
					if(yFlightNo.equals(currentFlightNo) && yflightHour.equals(currentHour)){
						isFlightNoEqual = true;
						if(cPrice < yPrice){
							//小于前一天,下降
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_DESCEND);
						}else if(cPrice > yPrice){
							//大于前一天,上升
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_ASCEND);
						}else {
							//等于前一天,持平
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
						}
					}
						
				}
				if(isFlightNoEqual==false){
					planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
				}
			
			}
		}else{
			//没有数据运价走势持平
				
				for (PlaneInfo planeInfo : list) {
				planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
			}
		}
		
		
		//对仓位重新组织，将cabin_type一样的仓位归类
		for (PlaneInfo planeInfo : list) {
			List<CabinEntityVO> cabins = planeInfo.getCabins();
			if(cabins != null && cabins.size()>0){
				for (CabinEntityVO cabinEntityVO : cabins) {
					if(map.containsKey(cabinEntityVO.getCabinType())){
						map.get(cabinEntityVO.getCabinType()).add(cabinEntityVO);
					}else{
						List<CabinEntityVO> putList = new ArrayList<CabinEntityVO>();
						putList.add(cabinEntityVO);
						map.put(cabinEntityVO.getCabinType(), putList);
					}
				}
			}
			
			//重新放到
			Set<Map.Entry<String, List<CabinEntityVO>>> set = map.entrySet();
	        for (Iterator<Map.Entry<String, List<CabinEntityVO>>> it = set.iterator(); it.hasNext();) {
	            Map.Entry<String, List<CabinEntityVO>> entry = (Map.Entry<String, List<CabinEntityVO>>) it.next();
	            planeInfo.getClassifyCabins().add(entry.getValue());
	        }		
			map.clear();
		}
		
		
		return list;
	}

	/**
	 * 将日期减一天
	 * @param flightStr
	 * @return
	 */
	private String getDate(String flightStr){
		if(null ==flightStr) return null;
		String yesterDate = null;
		Calendar cal = Calendar.getInstance();
		Date flightDate = DateUtils.StringToDate("yyyy-MM-dd", flightStr);
		cal.setTime(flightDate);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		yesterDate = DateUtils.formatDay(cal.getTime(), "yyyy-MM-dd");
		//return rowKey.substring(0, 21)+yesterDate.substring(0,8)+rowKey.substring(28);
		return yesterDate;
	}
	

	private String getYesterdayRowKey(String rowKey){
		String flightStr = null;
		String yesterDate = null;
		if(rowKey.length()==39){
			flightStr = rowKey.substring(21,28);
		}else{
			//若rowkey拼不对则直接返回
			return null;
		}
		
		Calendar cal = Calendar.getInstance();
		Date flightDate = DateUtils.StringToDate("yyyyMMdd", flightStr);
		cal.setTime(flightDate);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		yesterDate = DateUtils.formatDay(cal.getTime(), "yyyyMMdd");
		return rowKey.substring(0, 21)+yesterDate.substring(0,8)+rowKey.substring(28);
	}

	@Override
	public PlaneInfo findInfoToExcel(String tableName,
			String startRowKey, String endRowKey, List<Filter> list,String currentDate) {
		//暂时注释
		//System.out.println("startRow "+startRowKey +"  "+ "endRow: "+endRowKey);
		List<PlaneInfo> temp = null;
		FreightPriceEntity entity = new FreightPriceEntity();
		temp = entity.findByConditionsToPlaneInfo(tableName, startRowKey, endRowKey, list);
		//temp = entity.findByCustomFilters(tableName, startRowKey, endRowKey, null, null);
		if(temp.size()>0){
			//判断是否为当天数据,是则返回该数据，否则返回null
			if(currentDate.substring(5, 10).equals(temp.get(0).getGrapDateStr().substring(5, 10))){
				return temp.get(0);
			}
		}
		return null;
		
		/*System.out.println("startRow "+startRowKey +"  "+ "endRow: "+endRowKey);
		Random rand = new Random();
		int num = rand.nextInt();
		if(num % 2 == 0){
			PlaneInfo planeinfo = new PlaneInfo();
			planeinfo.setFromCity("PEK");
			planeinfo.setToCity("JFK");
			planeinfo.setFlightId("CA981");
			planeinfo.setRoundTripPrice(11111.0);
			return planeinfo;
		}else{
			PlaneInfo planeinfo = new PlaneInfo();
			planeinfo.setFromCity("PEK");
			planeinfo.setToCity("JFK");
			planeinfo.setFlightId("CA981");
			planeinfo.setRoundTripPrice(100.0);
			return planeinfo;
		}*/
		
	}
	/**
	 * 国航国际excel报表专用
	 */
	@Override
	public PlaneInfo findInfoToExcel(String tableName,
			String startRowKey, String endRowKey,String currentDate, List<HBaseFilterVO> voList, List<Filter> filters) {
		List<PlaneInfo> temp = null;
		FreightPriceEntity entity = new FreightPriceEntity();
		temp = entity.findByConditionsToPlaneInfo(tableName, startRowKey, endRowKey, voList, filters);
	//	AirChinaInternationalEntity airChina = new AirChinaInternationalEntity();
	//	airChina.findByCondition(reportDate, flightDate, fromCityCode, toCityCode, flightNo, carrierKey, hbaseKey);
		if(temp.size()>0){
			//判断是否为当天数据,是则返回该数据，否则返回null
			if(currentDate.substring(5, 10).equals(temp.get(0).getGrapDateStr().substring(5, 10))){
				return temp.get(0);
			}
		}
		return null;
				
	}
	
	public PlaneInfo findInfoToExcel(String hbaseKey, String fromCity,
			String toCity, String flightDate, String carrierKey,
			String currentDate, String flightNo) {
		PlaneInfo plane = null;
		List<AirChinaInternationalEntity> entityList = null;
		AirChinaInternationalEntity airChina = new AirChinaInternationalEntity();
		try {
			entityList = airChina.findByCondition(
					DateUtils.parseDate(currentDate, "yyyy-MM-dd"),
					DateUtils.parseDate(flightDate, "yyyyMMdd"), fromCity, toCity,
					flightNo, carrierKey, hbaseKey);
			plane =  convert2PlaneInfo(entityList);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return plane;
	}
	
	public PlaneInfo convert2PlaneInfo(List<AirChinaInternationalEntity> entityList){
		PlaneInfo plane = null;
		if(StringUtils.checkList(entityList)){
			 plane = new PlaneInfo();
			AirChinaInternationalEntity airChina = entityList.get(0);
			/*airChina.getFlightDate();
			airChina.getFlightNo();
			airChina.getFromCityCode();
			airChina.getToCityCode();
			airChina.getFromCityName();
			airChina.getToCityName();
			airChina.getPrice();*/
			
			plane.setFlightId(airChina.getFlightNo());
			plane.setFromCity(airChina.getFromCityCode());
			plane.setFromCityName(	airChina.getFromCityName());
			plane.setToCity(airChina.getToCityCode());
			plane.setToCityName(airChina.getToCityName());
			plane.setRoundTripPrice(""+airChina.getPrice());
			
		}
		
		return plane;
	}
	
	
	@Override
	public List<PlaneInfo> findByCustomFilters(String tableName,
			String startRowKey, String endRowKey, List<HBaseFilterVO> voList,
			List<Filter> filterList) {
		FreightPriceEntity entity = new FreightPriceEntity();	
		return  entity.findByConditionsToPlaneInfo(tableName, startRowKey, endRowKey, voList, filterList);
	}

	//for mysql
	@Override
	public List<PlaneInfo> findByConditions(String hbaseKey, String fromCity,
			String toCity, String flightBatchDate) {
		List<PlaneInfo> listVo=new ArrayList<PlaneInfo>();
		LineDayEntity tp=new LineDayEntity();
		List<LineDayEntity> listLins=tp.findList(hbaseKey, fromCity, toCity, flightBatchDate); 
		if(listLins!=null && listLins.size()>0){
			for(LineDayEntity entity :listLins){
				PlaneInfo vo=new PlaneInfo();
				vo.setFlightId(entity.getFlightNo());
				vo.setFromCity(entity.getFromCity());
				vo.setToCity(entity.getToCity());
				vo.setFromCityName(entity.getFromCityName());
				vo.setToCityName(entity.getToCityName());
				vo.setAreaCode(entity.getAreaCode());
				vo.setAreaName(entity.getAreaName());
				vo.setType(entity.getFlightType());
				vo.setSOURCE_ROWKEY(entity.getSourceRowkey());
				vo.setLowerPrice(entity.getTotalLowestPrice()>0.0?entity.getTotalLowestPrice()+"":"0");
				vo.setHighPrice(entity.getTotalHighestPrice()>0.0?entity.getTotalHighestPrice()+"":"0");
				vo.setLowprPrice(entity.getTotalLowestPrice()>0.0?entity.getTotalLowestPrice()+"":"0");
				vo.setAgentName(entity.getAgentName());
				vo.setCarrierFullName(entity.getCarrierFullName());
				vo.setCarrierName(entity.getCarrierName());
				vo.setCarrierKey(entity.getCarrierKey());
				vo.setGrabChannel(entity.getGrabChannelName());
				vo.setGrabChannelId(entity.getGrabChannelId()+"");
				vo.setHbaseKey(entity.getAttachHbaseKey());
				vo.setGrapDate(entity.getCreateTime());
				vo.setGrabDate(entity.getCreateTime() !=null ?DateUtils.dateToStr(entity.getGrabDate(), "yyyy-MM-dd HH:mm:ss"):null);//暂时把创建日期当作抓取日期（精确到“日”）
				vo.setActuallyFlightNo(entity.getActuallyFlightNo());
				vo.setFlightBatchDate(entity.getFlightDate());
				vo.setFlightBatchStartTime(entity.getStartTime() !=null ?DateUtils.dateToStr(entity.getStartTime(),"yyyy-MM-dd HH:mm:ss"):null);
				vo.setFlightBatchEndTime(entity.getEndTime() !=null ? DateUtils.dateToStr(entity.getEndTime(),"yyyy-MM-dd HH:mm:ss"):null);

				listVo.add(vo);
			}
		}
		
		return listVo;
	}

	@Override
	public List<PlaneInfo> findPlaneInfoProperty(String hbaseKey,
			String fromCity, String toCity, String flightBatchDate,
			String flightNo) {
		List<PlaneInfo> listVo=new ArrayList<PlaneInfo>();
		SinglePlaneInfoEntity tp=new SinglePlaneInfoEntity();
		List<SinglePlaneInfoEntity> listEntity=tp.findPlaneInfoProperty(hbaseKey, fromCity, toCity, flightBatchDate, flightNo);
		if(listEntity!=null && listEntity.size()>0){
			for(SinglePlaneInfoEntity entity:listEntity){
				PlaneInfo vo=new PlaneInfo();
				vo.setSOURCE_ROWKEY(entity.getSourceRowkey());
				vo.setGrabChannelId(entity.getGrabChannelId()+"");
				vo.setGrabChannel(entity.getGrabChannelName());
				vo.setCarrierName(entity.getCarrierName());
				vo.setCarrierKey(entity.getCarrierKey());
				vo.setCarrierFullName(entity.getCarrierFullName());
				vo.setFromCity(entity.getFromCity());
				vo.setFromCityName(entity.getFromCityName());
				vo.setToCity(entity.getToCity());
				vo.setToCityName(entity.getToCityName());
				vo.setFlightId(entity.getFlightNo());
				vo.setFlightBatchDate(entity.getFlightDate());
				vo.setActuallyFlightNo(entity.getActuallyFlightNo());
				vo.setType(entity.getFlightType());
				vo.setAreaCode(entity.getAreaCode());
				vo.setAreaName(entity.getAreaName());
				vo.setLowerPrice(entity.getTotalLowestPrice()>0.0?entity.getTotalLowestPrice()+"":"0");
				vo.setHighPrice(entity.getTotalHighestPrice()>0.0?entity.getTotalHighestPrice()+"":"0");
				vo.setLowprPrice(entity.getTotalLowestPrice()>0.0?entity.getTotalLowestPrice()+"":"0");
				vo.setAgentName(entity.getAgentName());
				vo.setGrapDate(entity.getGrabDate());
				vo.setGrabDate(DateUtils.dateToStr(entity.getGrabDate(), "yyyy-MM-dd HH:mm:ss"));//暂时把创建日期当作抓取日期（精确到“日”）
				vo.setActuallyFlightNo(entity.getActuallyFlightNo());
				vo.setFlightBatchStartTime(DateUtils.dateToStr(entity.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
				vo.setFlightBatchEndTime(DateUtils.dateToStr(entity.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
				
				listVo.add(vo);
			}
		}
		
		return listVo;
	}

}

class CabinComparator implements Comparator{

	@Override
	public int compare(Object obj1, Object obj2) {
		CabinEntityVO vo1 = (CabinEntityVO)obj1;
		CabinEntityVO vo2 = (CabinEntityVO)obj2;
		return vo1.getPrice().compareTo(vo2.getPrice());
	}
	

	
}